/**
 *  This file is part of MunchLevel.
 *
 *  MunchLevel is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MunchLevel is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MunchLevel.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.munchlevel.view.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.munchlevel.R;
import com.munchlevel.data.PlayerInfo;

public class PlayerAdapter extends ArrayAdapter<PlayerInfo> {

	public PlayerAdapter(Context context, int textViewResourceId, List<PlayerInfo> objects) {
		super(context, textViewResourceId, objects);
	}

	@Override
	public int getCount() {
		return super.getCount() + 1;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout listItem = null;

		if (convertView instanceof LinearLayout) {
			listItem = (LinearLayout) convertView;
		} else {
			listItem = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.player_list_item, null);
		}

		TextView name = (TextView) listItem.findViewById(R.id.player_name);
		TextView total = (TextView) listItem.findViewById(R.id.player_total);

		if (position < getCount() - 1) {

			PlayerInfo info = getItem(position);
			name.setText(info.getName());
			total.setText(Integer.toString(info.getLevel() + info.getGear()));
		} else {
			name.setText(R.string.check_paired_device);
			total.setText("");
		}
		return listItem;
	}

	public boolean isSelectDevice(int position) {
		return position == getCount() - 1;
	}

}
